import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-09-04
 * Time: 21:00
 */
public class test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] arr = new int[n];
        // 初始化
        for(int i = 0; i < n; i++) {
            arr[i] = scan.nextInt();
        }

        if(n == 1 && arr[0] == 1) {
            System.out.print(0);

        }else if(arr[0] != 1){
            System.out.println(-1);
        }

        int count = 0;
        int p1 = 0;
        Queue<Integer> queue = new LinkedList<>();

        int p2 = 1;
        while(p2 < n) {
            if(arr[p1] < arr[p2] && Math.abs(arr[p1] - arr[p2]) == 1) {
                // 符合的进入队列
                queue.add(p1);
                p1 = p2;
            }else {
                count++;
            }
            p2++;
        }
        // 最后还需要将 p1 添加进入
        queue.add(p1);

        for(int i = 1; i <= queue.size(); i++) {
            if(i != queue.poll()) {
                System.out.print(-1);
                return;
            }
        }

        System.out.print(count);

    }
}
